package com.no1ikun.view;

import com.no1ikun.publicdb.DB;
import com.no1ikun.util.MyJLabel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class MainView extends JFrame {
    private String title;
    private int width;
    private int height;
    private LeftJPanel left;
    private IndexJPanel index;
    private SelectJPanel select;
    private UpdateJPanel update;
    private DeleteJPanel delete;
    private InsertJPanel insert;
    private UpdatePunishment punishment;

    private MyJLabel time;
    private MyJLabel bottomText;

    //构造器
    public MainView(String title, int width, int height) {
        this.title = title;
        this.width = width;
        this.height = height;
        this.setResizable(false);

        //调用初始化方法,这个初始化方法是自己写的
        init();
    }

    //封装一个方法可以用来初始化窗口的基本信息
    public void init() {
        //1.设置标题
        this.setTitle(title);
        //要将窗口显示在屏幕的正中间:思路,先获取屏幕的宽高,让后再计算就可以
        //获取屏幕宽高
        double pmwidth = Toolkit.getDefaultToolkit().getScreenSize().getWidth();//获得屏幕的宽
        double pmheight = Toolkit.getDefaultToolkit().getScreenSize().getHeight();//获取屏幕的高
        //2.设置宽高以及显示的位置,将窗体居中显示this.getWidth():获取窗体的宽,this.getHeight()获取屏幕的高
        this.setBounds((int) (pmwidth - width) / 2, (int) (pmheight - height) / 2, width, height);
        //给窗口一个布局,绝对布局
        this.setLayout(null);
        //3.添加一个背景图片
        URL resource = LoginView.class.getResource("../img/bg.png");
        ImageIcon imageIcon = new ImageIcon(resource);
        JLabel bglable = new JLabel();
        bglable.setBounds(0, 0, 1000, 600);
        bglable.setIcon(imageIcon);

        //4.添加项目图标
        URL resource2 = MainView.class.getResource("../img/icon1.png");
        ImageIcon iconimg = new ImageIcon(resource2);
        Image image = iconimg.getImage();
        //5.添加背景
        JLabel bg = new JLabel();
        bg.setBounds(0, 0, 1000, 600);
        URL resource1 = MainView.class.getResource("../img/bg11.png");
        ImageIcon bgpic = new ImageIcon(resource1);
        bg.setIcon(bgpic);

        //6.中间的大标题
        MyJLabel centerText = new MyJLabel("欢迎使用学籍管理系统", 350, 60, 80, 10, "微软雅黑", Font.BOLD, 35, Color.WHITE);

        //7.左右两个白色面板
        left = new LeftJPanel();


        //8.右边部分(将5个界面都初始化出来)
        index = new IndexJPanel();
        insert = new InsertJPanel();
        update = new UpdateJPanel();
        delete = new DeleteJPanel();
        select = new SelectJPanel();
        punishment= new UpdatePunishment();



        //9.实时显示当前时间
        time = new MyJLabel("2022年12月10日 12:20:20 周二", 350, 60, 610, 12, "微软雅黑", 0, 16, Color.WHITE);
        //最下面的字

        bottomText = new MyJLabel("不积跬步无以至千里----远走的少年", 380, 60, 350, 515, "微软雅黑", 0, 12, Color.WHITE);

        //10闹钟的图标


        SimpleDateFormat sf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss E");

        //11.创建一个定时器
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {

                String timeText = sf.format(new Date());
                //完成操作
                time.setText(timeText);
            }
        };
        Timer timer = new Timer();
        timer.schedule(timerTask, 0, 1000);


        this.add(time);
        this.add(left);
        this.add(index);
        this.add(delete);
        this.add(insert);
        this.add(select);
        this.add(update);
        this.add(punishment);


        this.add(centerText);
        this.add(bg);
        this.setIconImage(image);
        this.setVisible(true);
        //监听关闭窗口事件
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                int option = JOptionPane.showConfirmDialog(null, "确定要退出到登录界面吗?", "温馨提示", JOptionPane.YES_NO_OPTION);
                //判断用户选择的是哪个按钮
                if (option == JOptionPane.YES_OPTION) {
                    //将登录界面显示出来
                    DB.loginView.setVisible(true);
                    //将自己隐藏
                    setVisible(false);
                } else {
                    //不做任何处理
                    setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
                }
            }
        });


    }

    //显示自己想要显示的界面
    public void showPage(String value) {
        //隐藏所有的
        clearPage();
        //显示指定的
        switch (value) {
            case "index":
                index.setVisible(true);
                break;
            case "update":
                update.setVisible(true);
                break;
            case "delete":
                delete.setVisible(true);
                break;
            case "select":
                select.setVisible(true);
                break;
            case "insert":
                insert.setVisible(true);
                break;

            case "punishment":
                punishment.setVisible(true);
                break;
        }
    }

    //隐藏所有界面
    public void clearPage() {
        //隐藏所有
        index.setVisible(false);
        select.setVisible(false);
        update.setVisible(false);
        delete.setVisible(false);
        insert.setVisible(false);
        punishment.setVisible(false);
    }
}
